package com.bjc.base.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.ResultVO;
import com.bjc.base.bean.request.OrderReqVO;
import com.bjc.base.bean.response.OrderVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.base.service.IOrderDetailService;
import com.bjc.base.service.IOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 许自辉
 * @since 2018-06-10
 */
@RestController
@RequestMapping("api/book")
@Api(value = "order-api", tags = { "订单相关接口" })
public class OrderController {

    @Autowired
    private IOrderService orderService;

	@ApiOperation("分页查询订单")
	@GetMapping("order/findPage")
	public ResultVO getOrderPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			OrderReqVO orderReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		List<OrderVO> orderVOs = orderService.getList(orderReqVO);
		return ResultGenerator.getSuccess(new PageInfo<OrderVO>(orderVOs));
	}
	
	@ApiOperation("分页查询订单")
	@GetMapping("order/find")
	public ResultVO getOrdere(OrderReqVO orderReqVO) {
		OrderVO orderVO = orderService.get(orderReqVO);
		return ResultGenerator.getSuccess(orderVO);
	}

	@ApiOperation("添加订单")
	@PostMapping("order/create")
	public ResultVO createOrder(@RequestBody OrderReqVO orderReqVOs) {
		int count = orderService.insert(orderReqVOs);
		return ResultGenerator.getInsertSuccess(count);
	}
	
	@ApiOperation("下单")
	@PostMapping("order/place")
	public ResultVO placeOrder(@RequestBody OrderReqVO orderReqVOs) {
		int orderId = orderService.placeOrder(orderReqVOs);
		return ResultGenerator.getSuccess(orderId);
	}

	@ApiOperation("更新订单")
	@PutMapping("order/update")
	public ResultVO updateOrder(@RequestBody OrderReqVO orderReqVOs) {
		int count = orderService.update(orderReqVOs);
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("批量删除订单")
	@DeleteMapping("order/deleteList")
	public ResultVO deleteOrder(@ApiParam(value = "order id集合") @RequestBody int[] ids) {
		int count = orderService.deleteList(ids);
		return ResultGenerator.getDeleteSuccess(count);
	}

}
